package com.ziyun.pop.web.modules.order.controller;

import com.ziyun.pop.common.pay.PayParamDto;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.order.dto.OrderDiscountDto;
import com.ziyun.pop.modules.order.service.FactoryOrderService;
import io.renren.common.utils.IPUtils;
import io.renren.common.utils.R;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 工厂订单
 * @author gaosheng
 * @date 2018-09-26 16:53
 */
@RestController
@RequestMapping("order/factoryorder")
public class FactoryOrderController {

    @Autowired
    private FactoryOrderService factoryOrderService;

    /**
     * 工厂订单列表
     */
    @RequestMapping("/sellerlist")
    public R list(@RequestParam Map<String, Object> params){
        // 供应商id
        params.put("sellerId", UserUtils.getCurentUser().getSellerId());
        return factoryOrderService.queryPageSellerFactoryOrderList(params);
    }

    /**
     * 工厂订单列表(买家 门店系统)
     */
    @RequestMapping("/buyerlist")
    public R buyerlist(@RequestParam Map<String, Object> params){
        // 供应商id
        params.put("sellerId", UserUtils.getCurentUser().getSellerId());
        return factoryOrderService.queryPageBuyerFactoryOrderList(params);
    }

    /**
     * 详情
     * @param orderId
     * @return
     */
    @RequestMapping("/orderInfo/{orderId}")
    public R orderInfo(@PathVariable("orderId") String orderId){
        return factoryOrderService.orderInfo(orderId);
    }

    /**
     * 报价订单商品审核
     * @return
     */
    @RequestMapping("/audit")
    public R audit(@RequestParam("itemId") Integer itemId){
        return factoryOrderService.audit(itemId);
    }

    /**
     * 报价订单商品详情展示
     * @param orderId
     * @return
     */
    @RequestMapping("/orderItemInfo")
    public R orderItemInfo(@RequestParam("orderId") String orderId){
        return factoryOrderService.orderItemInfo(orderId);
    }

    /**
     * 报价订单商品制作进度
     * @param orderNo
     * @return
     */
    @RequestMapping("/orderItemProgress")
    public R orderItemProgress(@RequestParam("orderNo") String orderNo){
        return factoryOrderService.orderItemProgress(orderNo);
    }

    /**
     * 工厂订单取消
     * @param orderId
     * @return
     */
    @RequestMapping("/orderCancel")
    public R orderCancel(@RequestParam("orderId") String orderId){
        return factoryOrderService.orderCancel(orderId);
    }

    /**
     * 工厂订单支付
     * @param orderId
     * @return
     */
    @RequestMapping("/orderPay")
    public R orderPay(@RequestParam("orderId") String orderId){
        return factoryOrderService.orderPay(orderId);
    }

    /**
     * 工厂订单实际联动支付
     * @param payParamDto
     * @return
     */
    @RequestMapping("/orderPayUmf")
    public R orderPayUmf(@RequestBody PayParamDto payParamDto,HttpServletRequest request){
        Long mallUserId =  UserUtils.getCurentUser().getMallUserId();
        String ip =  IPUtils.getIpAddr(request);
        return factoryOrderService.orderPayUmf(payParamDto,mallUserId,ip);
    }

    //同步回调地址
    @RequestMapping("orderPayUmfSyncCallback")
    public void orderPayUmfSyncCallback(HttpServletRequest request, HttpServletResponse response) throws Exception{
        // 成功则 进行跳转
        WebUtils.redirectToSavedRequest(request, response, "/index.html#modules/order/buyerfactoryorderinfo.html");
    }

    //异回调地址
    @RequestMapping("orderPayUmfAsyncCallback")
    public void orderPayUmfAsyncCallback(HttpServletRequest request, HttpServletResponse response) throws Exception{
        // 处理业务
        factoryOrderService.umfPayRecall(request, response);

    }

    /**
     * 工厂订单验货确定
     * @param orderId
     * @return
     */
    @RequestMapping("/orderReceive")
    public R orderReceive(@RequestParam("orderId") String orderId){
        return factoryOrderService.orderReceive(orderId);
    }

    /**
     * 工厂订单审核完成
     * @param orderId
     * @return
     */
    @RequestMapping("/orderFinish")
    public R orderFinish(@RequestParam("orderId") String orderId){
        return factoryOrderService.orderFinish(orderId);
    }

    /**
     * 订单调价
     * @param orderDiscountDto
     * @return
     */
    @RequestMapping("/orderDiscount")
    public R orderDiscount(@RequestBody OrderDiscountDto orderDiscountDto){
        String username = UserUtils.getCurentUser().getUsername();
        return factoryOrderService.orderDiscount(orderDiscountDto, username);
    }

    /**
     * 用友销售单号
     * @param orderId
     * @param yonyouOrderNo
     * @return
     */
    @RequestMapping("/orderYonyou")
    public R orderYonyou(@RequestParam("orderId") String orderId, @RequestParam("yonyouOrderNo") String yonyouOrderNo){
        return factoryOrderService.orderYonyou(orderId, yonyouOrderNo);
    }

    /**
     * 开始生产
     * @param orderId
     * @return
     */
    @RequestMapping("/produceStart")
    public R produceStart(@RequestParam("orderId") String orderId){
        return factoryOrderService.produceStart(orderId);
    }

    /**
     * 生产完成
     * @param orderId
     * @return
     */
    @RequestMapping("/produceFinish")
    public R produceFinish(@RequestParam("orderId") String orderId){
        return factoryOrderService.produceFinish(orderId);
    }

    /**
     * 小B工厂订单确认收货
     * @param orderId
     * @return
     */
    @RequestMapping("/confirmGetGoods")
    public R confirmGetGoods(@RequestParam("orderId") String orderId){
        return factoryOrderService.confirmGetGoods(orderId);
    }

    /**
     * 自营门店验货确定
     * @param orderId
     * @return
     */
    @RequestMapping("/confirmReceive")
    public R confirmReceive(@RequestParam("orderId") String orderId){
        return factoryOrderService.confirmReceive(orderId);
    }

    /**
     * 自营门店审核完成
     * @param orderId
     * @return
     */
    @RequestMapping("/auditFinish")
    public R auditFinish(@RequestParam("orderId") String orderId){
        return factoryOrderService.auditFinish(orderId);
    }

}
